//
//  EJSWithdrawalViewController.m
//  EnjoyShoppingStreet
//
//  Created by lifayu on 2018/1/23.
//  Copyright © 2018年 lifayu. All rights reserved.
//

#import "EJSWithdrawalViewController.h"
#import "EJSWithdrawsRequest.h"
#import "EJSUserBalanceRequest.h"

@interface EJSWithdrawalViewController ()<UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UILabel *wechaLabel;
@property (weak, nonatomic) IBOutlet UITextField *textField;
@property (weak, nonatomic) IBOutlet UILabel *balanceLabel;

@end

@implementation EJSWithdrawalViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self getUserBalance];
}

- (void)configOwnViews{
    self.title = @"提现";
    self.balanceLabel.text = [NSString stringWithFormat:@"可用余额：%.2f",self.balance.floatValue];
}

- (void)getUserBalance{
    FlyWeakSelf
    EJSUserBalanceRequest *req= [EJSUserBalanceRequest new];
    req.hideLoadingView = YES;
    [req asyncRequestWithsuccessHandler:^(BaseResponse *response) {
        EJSUserBalanceModel *model = response.data;
        weakSelf.balanceLabel.text = [NSString stringWithFormat:@"可用余额：%.2f",model.money.floatValue];
        weakSelf.balance = model.money;
    } failHandler:^(BaseResponse *response) {
        [[HUDHelper sharedInstance] tipMessage:response.message];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)allWithdrawalAction:(UIButton *)sender {
    self.textField.text = self.balance;
    [self checkInputContent];
}

- (IBAction)withdrawalAction:(UIButton *)sender {
    NSString *text = self.textField.text;
    if(text && text.length > 0){
       text = [text stringByReplacingOccurrencesOfString:@"€ " withString:@""];
        if(text.floatValue <= 0){
            [[HUDHelper sharedInstance] tipMessage:@"提现金额必须大于0"];
            return;
        }
        if(text.floatValue > self.balance.floatValue){
            [[HUDHelper sharedInstance] tipMessage:@"提现金额大于可用余额"];
            return;
        }
        EJSWithdrawsRequest *req = [EJSWithdrawsRequest new];
        req.amount = text;
        [req asyncRequestWithsuccessHandler:^(BaseResponse *response) {
             [[HUDHelper sharedInstance]tipMessage: @"提现成功"];
            [self getUserBalance];
            [self.navigationController popViewControllerAnimated:YES];
        } failHandler:^(BaseResponse *response) {
            [[HUDHelper sharedInstance]tipMessage: response.message];
        }];
    }else{
        [[HUDHelper sharedInstance] tipMessage:@"请输入提现金额"];
    }
}

- (IBAction)textFieldChanged:(UITextField *)sender {
    [self checkInputContent];
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    [self checkInputContent];
    return YES;
}

- (void)checkInputContent{
    NSString *text = self.textField.text;
    if(text && text.length > 0){
        if(![text hasPrefix:@"€"]){
            text = [text stringByReplacingOccurrencesOfString:@"€ " withString:@""];
            self.textField.text = [NSString stringWithFormat:@"€ %@",text];
        }
    }
}

@end
